﻿using System;
using System.Collections.Generic;
using DB;
using ViewModel;

namespace Logic
{
    /// <summary>
    /// SampleDBのLogicクラス
    /// </summary>
    public class SampleDBLogic
    {
        #region 定数
        /// <summary>取得用SQLファイル</summary>
        private const string SELECT_TABLE = @"DB.SQL.Select_T_SAMPLE.sql";
        /// <summary>追加用SQLファイル</summary>
        private const string INSERT_TABLE = @"DB.SQL.Insert_T_SAMPLE.sql";
        /// <summary>更新用SQLファイル</summary>
        private const string UPDATE_TABLE = @"DB.SQL.Update_T_SAMPLE.sql";
        /// <summary>削除用SQLファイル</summary>
        private const string DELETE_TABLE = @"DB.SQL.Delete_T_SAMPLE.sql";
        #endregion

        #region public
        /// <summary>
        /// データを取得する
        /// </summary>
        /// <returns></returns>
        public List<GridData> GetData()
        {
            List<GridData> gridDataList = new List<GridData>();
        /// 列の定義
             string[] columns = new string[] { "ID", "UPDATE_TIME", "CONTENTS", "HIGHT", "Wight", "BP_LOW", "BP_HIGH", "SLEEPTIME" };
            var tb = DBLogic.ExecuteReader(SELECT_TABLE, columns);

            if (tb == null || tb.Rows == null || tb.Rows.Count <= 0) 
            {
                return gridDataList;
            }

            // GridDataに格納する
            for (int rowIdx = 0; rowIdx < tb.Rows.Count; rowIdx++)
            {
                int id;
                int.TryParse(tb.Rows[rowIdx]["ID"] as string ?? String.Empty, out id);
                DateTime upd;
                DateTime.TryParse(tb.Rows[rowIdx]["UPDATE_TIME"] as string ?? String.Empty, out upd);
                var contents = tb.Rows[rowIdx]["CONTENTS"] as string;
               var hight = tb.Rows[rowIdx]["HIGHT"] as string;
                var Wight = tb.Rows[rowIdx]["Wight"] as string;
                var BP_LOW= tb.Rows[rowIdx]["BP_LOW"] as string;
                var BP_HIGH = tb.Rows[rowIdx]["BP_HIGH"] as string;
                var SLEEPTIME = tb.Rows[rowIdx]["SLEEPTIME"] as string;
                gridDataList.Add(new GridData() { ID = id, Hight = hight, Wight = Wight, Contents = contents, UpdateTime = upd, BP_LOW = BP_LOW, BP_HIGH = BP_HIGH, SLEEPTIME = SLEEPTIME });
              
            }

            return gridDataList;
        }

        /// <summary>
        /// DBに登録する
        /// </summary>
        /// <param name="contents">コンテンツ</param>
        //public void Regist(string contents)
        public void Regist(string id,string contents, string Hight, string Wight,string BP_LOW,string BP_HIGH,string SLEEPTIME)
        {

            // パラメータ
            Dictionary<string, object> paramDic = new Dictionary<string, object>();
            paramDic.Add("CONTENTS", contents);
            paramDic.Add("HIGHT", Hight);
            paramDic.Add("Wight", Wight);
            paramDic.Add("BP_LOW", BP_LOW);
            paramDic.Add("BP_HIGH", BP_HIGH);
            paramDic.Add("SLEEPTIME", SLEEPTIME);
            paramDic.Add("UPDATE_TIME", DateTime.Now);
         

            // SQL実行
            DBLogic.ExecuteNonQuery(INSERT_TABLE, paramDic);
        }

        #endregion

        public void update(string id,string contents, string Hight, string Wight, string BP_LOW, string BP_HIGH, string SLEEPTIME)
        {
            // パラメータ
            Dictionary<string, object> paramDic = new Dictionary<string, object>();
            paramDic.Add("ID", id);
            paramDic.Add("HIGHT", Hight);
            paramDic.Add("CONTENTS", contents);
            paramDic.Add("Wight", Wight);
            paramDic.Add("BP_LOW", BP_LOW);
            paramDic.Add("BP_HIGH", BP_HIGH);
            paramDic.Add("SLEEPTIME", SLEEPTIME);
            paramDic.Add("UPDATE_TIME", DateTime.Now);

            // SQL実行
            DBLogic.ExecuteNonQuery(UPDATE_TABLE, paramDic);
        }

        public void delete(string id)
        {
            // パラメータ
            Dictionary<string, object> paramDic = new Dictionary<string, object>();
            paramDic.Add("ID", id);

            // SQL実行
            DBLogic.ExecuteNonQuery(DELETE_TABLE, paramDic);
        }

    }
}
